package com.learn.file;

import org.junit.Test;

import java.io.*;

public class ObjectInputOutput {
    //序列化
    @Test
    public void objectOutputTest() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.data"));
        oos.writeObject(new String("aaaa"));
        oos.flush();
        oos.writeObject(new Person("xxxx",22));
        oos.flush();
        oos.close();
    }
    //反序列化
    @Test
    public void inputObjectTest() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.data"));
        Object readObject = ois.readObject();
        String result = (String)readObject;
        Person p = (Person) ois.readObject();
        System.out.println(result);
        System.out.println(p);
        ois.close();
    }
}
